﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public static class PublishDataSourceToFileSystemThread
    {
        private static bool s_isRunning;
        private static Thread s_thread;
        
        private static string s_targetDir;
        private static List<ServiceProxy.DesignServiceRef.PublishedDataSource> DataSourceList;


        private static List<ServiceProxy.DesignServiceRef.PublishedDataSource> s_successList;

        private static List<ServiceProxy.DesignServiceRef.PublishedDataSource> s_failureList;

        private static Action<List<ServiceProxy.DesignServiceRef.PublishedDataSource>, List<ServiceProxy.DesignServiceRef.PublishedDataSource>> s_completed;


        public static void Start(List<ServiceProxy.DesignServiceRef.PublishedDataSource> dataSourceList, string targetDir, Action<List<ServiceProxy.DesignServiceRef.PublishedDataSource>, List<ServiceProxy.DesignServiceRef.PublishedDataSource>> completed)
        {
            if (s_isRunning)
            {
                return;
            }
            s_targetDir = targetDir;

            s_successList = new List<ServiceProxy.DesignServiceRef.PublishedDataSource>();
            s_failureList = new List<ServiceProxy.DesignServiceRef.PublishedDataSource>();

            s_completed = completed;

            DataSourceList = dataSourceList;

            s_isRunning = true;
            s_thread = new Thread(Running);
            s_thread.Start();
        }

        public static void Stop()
        {
            if (s_isRunning && s_thread == null)
            {
                s_thread.Abort();
                s_isRunning = false;
                s_thread = null;
            }
        }

        private static void Running()
        {
            // 循环数据源进行发布
            foreach (var dataSource in DataSourceList)
            {
                Coolzon.Metadata.DataSource rtDataSource = Convert(dataSource);

                // 生成数据源文件
                if (Coolzon.Designtime.Metadata.Storage.SaveRTDataSourceFile(s_targetDir + "DataSource\\" + rtDataSource.Name + ".xml", rtDataSource))
                {
                    // 成功
                    s_successList.Add(dataSource);
                }
                else
                {
                    // 添加到失败的列表中
                    s_failureList.Add(dataSource);
                }
            }

            if (s_completed != null)
            {
                s_completed(s_successList, s_failureList);
            }
            s_isRunning = false;
            s_thread = null;
        }
        public static Coolzon.Metadata.DataSource Convert(ServiceProxy.DesignServiceRef.PublishedDataSource mDataSource)
        {
            Coolzon.Metadata.DataSource dataSource = new Metadata.DataSource();
            dataSource.Name = mDataSource.Name;
            dataSource.Caption = mDataSource.Caption;
            dataSource.Type = mDataSource.Type;
            dataSource.Version = mDataSource.PublishedVersion;
            dataSource.RuntimeVersion = mDataSource.RTVersion;
            dataSource.CacheMode = Coolzon.Metadata.DataSource.TryParseCacheMode("Share");
            dataSource.Address = mDataSource.Address;

            dataSource.Authentication = new Metadata.Authentication();
            dataSource.Authentication.User = mDataSource.AuthenticationUser;
            dataSource.Authentication.Mode = mDataSource.AuthenticationMode;
            dataSource.Authentication.Password = mDataSource.AuthenticationPassword;

            dataSource.DataSets = new List<Metadata.DataSet>();
            //data set
            foreach (var mDataSet in mDataSource.DataSet)
            {
                Coolzon.Metadata.DataSet dataSet = new Metadata.DataSet();
                dataSet.CacheMode = Coolzon.Metadata.DataSource.TryParseCacheMode("Share");
                dataSet.Caption = mDataSet.Caption;
                dataSet.Name = mDataSet.Name;
                dataSet.Type = mDataSet.Type;
                dataSet.ValueString = mDataSet.ValueString;
                dataSet.Parameters = new List<Metadata.Parameter>();

                // parameters
                foreach (var mParameter in mDataSet.Parameters)
                {
                    Coolzon.Metadata.Parameter parameter = new Metadata.Parameter();

                    parameter.Caption = mParameter.Caption;
                    parameter.Name = mParameter.Name;
                    parameter.ValueType = mParameter.ValueType;

                    dataSet.Parameters.Add(parameter);
                }

                dataSource.DataSets.Add(dataSet);
            }

            return dataSource;
        }
    }
}
